﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DbConnect;
using DbConnect.Sql;


namespace DnDEngine.Class
{
    public class Website
    {
        #region "Private attributes"

        private List<Request> _lRequest;

        private int _ID;

        #endregion

        #region "Properties"

        public String Name
        {
            get;
            set;
        }

        public String Url
        {
            get;
            set;
        }

        public String MainPage
        {
            get;
            set;
        }

        public SiteType SiteType
        {
            get;
            set;
        }

        public List<Request> lRequest
        {
            get
            {
                return _lRequest;
            }
        }

        public int Index
        {
            get
            {
                return this._ID;
            }
        }

        public int NbRequest
        {
            get;
            set;
        }

        #endregion

        #region "Events"
        #endregion

        #region "Public functions"

        public Website(int ID)
        {
            this._ID = ID;
            this.LoadWebsite();
        }

        public Website(String Name, String Url)
        {
            try
            {
                Logger.Log.TraceAction("WebSite.Constructor", "");

                this.Name = Name;
                this.Url = Url;
                _lRequest = new List<Request>();
            }
            catch (Exception ex)
            {
                Logger.Log.TraceError("Website.Constructor", ex);
            }
        }

        public void AddRequest(Request req)
        {
            try
            {
                Logger.Log.TraceAction("WebSite.AddRequest", req.Url);

                this.lRequest.Add(req);
            }
            catch (Exception ex)
            {
                Logger.Log.TraceError("WebSite.AddRequest", ex);
            }
        }

        public void DeleteRequest(int index)
        {
            try
            {
                Logger.Log.TraceAction("WebSite.DeleteRequest", index.ToString());

                foreach (var req in _lRequest)
                {
                    if (req.Index == index)
                    {
                        _lRequest.Remove(req);
                        break;
                    }
                }
            }
            catch (Exception ex)
            {
                Logger.Log.TraceError("WebSite.DeleteRequest", ex);
            }
        }

        public int Save()
        {
            return this.InsertWebsite();
        }

        public Boolean Delete()
        {

            var Ok = false;
            try
            {
                Logger.Log.TraceAction("Website.Delete", "");
                foreach (var req in _lRequest)
                {
                    req.Delete();
                }
                this.DeleteWebsite();
                Ok = true;
            }
            catch (Exception ex)
            {
                Logger.Log.TraceError("Website.Delete", ex);
                Ok = false;
            }
            finally
            {
            }
            return Ok;
        }

        public Boolean Update(Website Cpy)
        {

            var Ok = false;
            try
            {
                Logger.Log.TraceAction("Website.Update", "");
                this.Name = Cpy.Name;
                this.SiteType = Cpy.SiteType;
                this.Url = Cpy.Url;
                this.MainPage = Cpy.MainPage;
                this._lRequest = Cpy.lRequest;
                this.NbRequest = Cpy.NbRequest;
                UpdateWebsite();
                Ok = true;
            }
            catch (Exception ex)
            {
                Logger.Log.TraceError("Website.Update", ex);
                Ok = false;
            }
            finally
            {
            }
            return Ok;
        }

        public override string ToString()
        {
            return this.Name;
        }

        #endregion

        #region "Sql Request"

        private int InsertWebsite()
        {
            var Ok = 0;
            try
            {
                Logger.Log.TraceAction("WebSite.Insert", "");
                var req = SqlWebsite.InsertInto;

               
                var data = new DbConnect.DataAccess();
                 data.AddParam(ParamWebsite.Name, this.Name);
                 data.AddParam(ParamWebsite.Url, this.Url);
                 data.AddParam(ParamWebsite.Mainpage, this.MainPage);
                 data.AddParam(ParamWebsite.SiteType, this.SiteType.ID);
                Ok = data.Insert(req);

                this._ID = data.GetLastIDInserted(SqlWebsite.TableName);
                
                data = null;
            }
            catch (Exception ex)
            {
                Logger.Log.TraceError("WebSite.Insert", ex);
                Ok = 0;
            }
            return Ok;

        }

        private void DeleteWebsite()
        {
            try
            {
                Logger.Log.TraceAction("WebSite.DeleteWebsite", "");
                // delete from website where ID = @ID
                var req = SqlWebsite.Delete;

                var data = new DbConnect.DataAccess();
               
                 data.AddParam(ParamWebsite.ID, this._ID);
                data.Delete(req);
                data = null;
                
            }
            catch (Exception ex)
            {
                Logger.Log.TraceError("WebSite.DeleteWebsite", ex);
            }


        }

        private void UpdateWebsite()
        {
            try
            {
                Logger.Log.TraceAction("WebSite.Update", "");
                var req = SqlRequest.Update;

                var data = new DbConnect.DataAccess();

               
                 data.AddParam(ParamWebsite.ID, this._ID);
                 data.AddParam(ParamWebsite.Name, this.Name);
                 data.AddParam(ParamWebsite.Url, this.Url);
                 data.AddParam(ParamWebsite.Mainpage, this.MainPage);
                 data.AddParam(ParamWebsite.SiteType, this.SiteType.ID);
                 data.AddParam(ParamWebsite.NbRequest, this._lRequest.Count());

                data.Delete(req);
                data = null;
                
            }
            catch (Exception ex)
            {
                Logger.Log.TraceError("WebSite.update", ex);
            }


        }
        
        private void LoadWebsite()
        {
            Logger.Log.TraceAction("Website.LoadWebSite", "");
            try
            {
                var req = SqlWebsite.Select;
               
                var data = new DataAccess();
                 data.AddParam(ParamWebsite.ID, this._ID);
                var table = data.SelectWithParam(req);

                if (table.Rows.Count == 1)
                {
                    this.Name = table.Rows[0].GetStringValue("wName");
                    this.Url = table.Rows[0].GetStringValue("Url");
                    this.MainPage = table.Rows[0].GetStringValue("Mainpage");
                    this.SiteType = new SiteType(table.Rows[0].GetIntValue("Site_Type"));
                }
            }
            catch (Exception ex)
            {
                Logger.Log.TraceError("Website.LoadWebsite", ex);
            }

        }

        private int LoadWebsiteRequest()
        {
            var Ok = 0;
            Logger.Log.TraceAction("Website.LoadWebsiteRequest", "");
            try
            {
                var req = SqlWRequest.SelectByWebsite;
               
                var data = new DataAccess();
                 data.AddParam(ParamWRequest.WebSite, this._ID);

                var table = data.SelectWithParam(req);

                if (table.Rows.Count > 0)
                {
                    foreach (var row in table.Rows)
                    {
                        this.AddRequest(new Request(Int16.Parse(row.ToString())));
                    }
                }
                NbRequest = _lRequest.Count();
                Ok = 1;
            }
            catch (Exception ex)
            {
                Logger.Log.TraceError("Website.LoadWebsiteRequest", ex);
            }
            return Ok;
        }

        

        #endregion

        #region "Private functions"


        #endregion

        






    }
}
